//
//  InformationViewController.m
//  Frame
//
//  Created by 这是工作本 on 16/2/19.
//  Copyright © 2016年 这是工作本. All rights reserved.
//

#import "InformationViewController.h"

@interface InformationViewController () <UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSArray *detailArr;
@property (nonatomic, retain) NSMutableArray *InformationArr;

@end

@implementation InformationViewController

//懒加载

- (NSMutableArray *)InformationArr
{
    if (nil == _InformationArr) {
        
        self.InformationArr = [NSMutableArray array];
    }
    return _InformationArr;
}


- (NSArray *)detailArr
{
    if (nil == _detailArr) {
        
        self.detailArr = [NSArray array];
    }
    
    return _detailArr;
}


- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"资讯";
    [self getNetdata];
    //隐藏自动返回
    self.navigationItem.hidesBackButton = YES;
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:29 / 255.0 green:127 / 255.0  blue:140 / 255.0  alpha:1.0];
    
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"but_an.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    //设置title颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0],NSForegroundColorAttributeName,nil]];
    
    
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 13) style:UITableViewStylePlain];
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
//    self.tableV.backgroundColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    [self.view addSubview:self.tableV];
    
    //解决左边空白问题
    if ([self.tableV respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableV setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.tableV respondsToSelector:@selector(setLayoutMargins:)])
    {
        [self.tableV setLayoutMargins:UIEdgeInsetsZero];
    }
    

    
    // 添加下拉刷新
    self.tableV.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(referenceAction)];
    
    // 添加上拉加载
    self.tableV.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloateAction)];

    //数据
    
//    self.detailArr = [NSArray arrayWithObjects:@"999.png", @"999.png", @"999.png", @"999.png", @"999.png", @"999.png", @"999.png", @"999.png", nil];

    
    //添加全屏右滑动手势
    id target = self.navigationController.interactivePopGestureRecognizer.delegate;
    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    // 设置手势代理，拦截手势触发
    pan.delegate = self;
    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:pan];
    // 禁止使用系统自带的滑动手势
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;


    
}


//代理方法解决15像素空白问题

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}



- (void)referenceAction
{
    
}

- (void)reloateAction
{
    
}

- (void)getNetdata
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    
    [manager GET:@"http://192.168.1.157:8080/api/center?userId=3339WrRIAYfv9wPeGqC&equipment=ios&requestType=List_Article_Api&token=74214cd04c081efd29a1f55d3d7b6c3a" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        //解析数据封装
        
        NSArray *informationArr = [responseObject objectForKey:@"data"];
        for (NSDictionary * dic in informationArr) {
            Homepage *home = [[Homepage alloc] init];

            [home setValuesForKeysWithDictionary:dic];
            [self.InformationArr addObject:home];
            
        }
        
        [self.tableV reloadData];
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        NSLog(@"%@", error);
        
    }];

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85 * ScreenHeigth;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.InformationArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *cellStr = @"cell";
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        
        cell = [[InformationTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    
    Homepage *home = [self.InformationArr objectAtIndex:indexPath.row];
    
    
    [cell data:home];
    //清除选中色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSDictionary *dict = @{@"id":@"EF0B0bKUj0J64BZruk7"};
    
    [manager POST:@"http://192.168.1.157:8080/api/center?userId=b5yTocc9TFblhoGBOW9&equipment=ios&requestType=View_Article_Api&token=5d8c12af8b55699306474591db23e05d" parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
       
        
       
        
        
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        NSLog(@"%@", error);
        
    }];
    
    DetailInformationViewController *detail = [[DetailInformationViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
    
    
}




- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
